package com.light.buttons;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;

import com.light.buttons.Button.ButtonState;
import com.light.roller.Direction;
import com.light.roller.Player;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class RightButton extends Button
{
	protected Player m_player;
	
	protected boolean m_clicked = false;
	
	protected float[] m_positionXs = { Roller.CAMERA_WIDTH * 0.12f,
			   			               Roller.CAMERA_WIDTH * 0.945f,
			   			               Roller.CAMERA_WIDTH * 0.055f,
			   			               Roller.CAMERA_WIDTH * 0.88f };

    protected float[] m_positionYs = { Roller.CAMERA_HEIGHT * 0.925f,
			   						   Roller.CAMERA_HEIGHT * 0.82f,
			   						   Roller.CAMERA_HEIGHT * 0.18f,
			   						   Roller.CAMERA_HEIGHT * 0.075f };
    
	protected float[] m_position1Xs = { Roller.CAMERA_WIDTH * 0.17f,
						               Roller.CAMERA_WIDTH * 0.93f,
						               Roller.CAMERA_WIDTH * 0.07f,
						               Roller.CAMERA_WIDTH * 0.83f };

	protected float[] m_position1Ys = { Roller.CAMERA_HEIGHT * 0.9f,
									   Roller.CAMERA_HEIGHT * 0.77f,
									   Roller.CAMERA_HEIGHT * 0.23f,
									   Roller.CAMERA_HEIGHT * 0.1f };

	protected float[] m_position2Xs = { Roller.CAMERA_WIDTH * 0.21f,
							           Roller.CAMERA_WIDTH * 0.926f,
							           Roller.CAMERA_WIDTH * 0.074f,
							           Roller.CAMERA_WIDTH * 0.79f };

	protected float[] m_position2Ys = { Roller.CAMERA_HEIGHT * 0.88f,
									   Roller.CAMERA_HEIGHT * 0.685f,
									   Roller.CAMERA_HEIGHT * 0.315f,
									   Roller.CAMERA_HEIGHT * 0.12f };
    
	private float m_onScreenPositionX;
	private TextureRegion m_greyTextureRegion;
	private TiledTextureRegion m_tiledTextureRegion;
	private AnimatedSprite m_animatedSprite;
	private Sprite m_greySprite;
	private float m_onScreenPositionY;
	
    public float getOnScreenPositionX() { return m_onScreenPositionX; }
    public float getOnScreenPositionY() { return m_onScreenPositionY; }

	public RightButton(BaseGameActivity game, Player player) 
	{
		super(game);
		m_player = player;
		m_state = Button.ButtonState.NOT_CONTROLABLE;
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (m_animatedSprite != null)
		{
			float[] tempPosX = m_positionXs;
			float[] tempPosY = m_positionYs;
			switch (Button.NOW_SIZE)
			{
			case 0:
				tempPosX = m_positionXs;
				tempPosY = m_positionYs;
				break;
			case 1:
				tempPosX = m_position1Xs;
				tempPosY = m_position1Ys;
				break;
			case 2:
				tempPosX = m_position2Xs;
				tempPosY = m_position2Ys;
				break;
			}
	    	float[] accelerometer_values = event.values.clone();
	    	if (Roller.orientation == Configuration.ORIENTATION_PORTRAIT)
	    	{
		    	if (Math.abs(accelerometer_values[1]) >= Math.abs(accelerometer_values[0]))
		    	{
		    		if (accelerometer_values[1] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.LEFT)
		    		{
		    			int centerX = (int)(tempPosX[2] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[2] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.LEFT;
		    			m_animatedSprite.setRotation(90);
		    			m_greySprite.setRotation(90);
		    		}
		    		else if (accelerometer_values[1] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.RIGHT)
		    		{
		    			int centerX = (int)(tempPosX[1] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[1] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.RIGHT;
		    			m_animatedSprite.setRotation(-90);
		    			m_greySprite.setRotation(-90);
		    		}
		    	}
		    	else
		    	{
		    		if (accelerometer_values[0] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.DOWN)
		    		{
		    			int centerX = (int)(tempPosX[3] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[3] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.DOWN;
		    			m_animatedSprite.setRotation(180);
		    			m_greySprite.setRotation(180);
		    		}
		    		else if (accelerometer_values[0] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.UP)
		    		{
		    			int centerX = (int)(tempPosX[0] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[0] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.UP;
		    			m_animatedSprite.setRotation(0);
		    			m_greySprite.setRotation(0);
		    		}
		    	}
	    	}
	    	else
	    	{
		    	if (Math.abs(accelerometer_values[1]) >= Math.abs(accelerometer_values[0]))
		    	{
		    		if (accelerometer_values[1] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.DOWN)
		    		{
		    			int centerX = (int)(tempPosX[3] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[3] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.DOWN;
		    			m_animatedSprite.setRotation(180);
		    			m_greySprite.setRotation(180);
		    		}
		    		else if (accelerometer_values[1] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.UP)
		    		{
		    			int centerX = (int)(tempPosX[0] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[0] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.UP;
		    			m_animatedSprite.setRotation(0);
		    			m_greySprite.setRotation(0);
		    		}
		    	}
		    	else
		    	{
		    		if (accelerometer_values[0] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.RIGHT)
		    		{
		    			int centerX = (int)(tempPosX[1] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[1] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.RIGHT;
		    			m_animatedSprite.setRotation(-90);
		    			m_greySprite.setRotation(-90);
		    		}
		    		else if (accelerometer_values[0] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.LEFT)
		    		{
		    			int centerX = (int)(tempPosX[2] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(tempPosY[2] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.LEFT;
		    			m_animatedSprite.setRotation(90);
		    			m_greySprite.setRotation(90);
		    		}
		    	}
	    	}
		}
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		Camera tempCamera = Roller.cameraManager.getCamera();
    	setByCameraPosition(tempCamera.getCenterX(), tempCamera.getCenterY());
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources("RightButton", m_game);
		ResourceManager.onLoadResources("RightButtonGrey", m_game);
		this.m_tiledTextureRegion = ResourceManager.getTiledTextureRegion("RightButton");
		this.m_greyTextureRegion = ResourceManager.getTextureRegion("RightButtonGrey");
	}

	@Override
	public void unLoadResources() {
		this.m_tiledTextureRegion = null;
		ResourceManager.unLoadResources("RightButton", m_game);
	}
	
	@Override
	public void greyButton()
	{
		m_greySprite.setVisible(true);
		hideButton();
	}
	@Override
	public void ungreyButton()
	{
		showButton();
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		
		int centerX = (int)(m_positionXs[0] - this.m_tiledTextureRegion.getWidth() / 2);
        int centerY =  (int)(m_positionYs[0] - this.m_tiledTextureRegion.getHeight() / 2);
        m_onScreenPositionX = centerX;
        m_onScreenPositionY = centerY;
        m_animatedSprite = new AnimatedSprite(centerX, centerY, this.m_tiledTextureRegion)
		{
			@Override
        	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
        	{
				Roller.debugger.showText("right_touched");
				if (pSceneTouchEvent.isActionDown())
				{
					switch (m_state)
					{
					case CONTROLABLE:
						m_player.rightClicked = true;
						m_animatedSprite.setCurrentTileIndex(1);
						break;
					default:
						Roller.debugger.showText("status_error");
					}
				}
				else if (pSceneTouchEvent.isActionUp())
				{
					switch (m_state)
					{
					case CONTROLABLE:
						m_player.rightClicked = false;
						m_animatedSprite.setCurrentTileIndex(0);
						break;
					default:
						Roller.debugger.showText("status_error");
					}
				}
				else if (pSceneTouchEvent.isActionOutside())
				{
					switch (m_state)
					{
					case CONTROLABLE:
						m_player.rightClicked = false;
						m_animatedSprite.setCurrentTileIndex(0);
						break;
					default:
						Roller.debugger.showText("status_error");
					}
				}
				else if (pSceneTouchEvent.isActionCancel())
				{
					switch (m_state)
					{
					case CONTROLABLE:
						m_player.rightClicked = false;
						m_animatedSprite.setCurrentTileIndex(0);
						break;
					default:
						Roller.debugger.showText("status_error");
					}
				}
				if (pTouchAreaLocalX >= m_animatedSprite.getWidth() ||
						pTouchAreaLocalX <= 0 ||
						pTouchAreaLocalY >= m_animatedSprite.getHeight() ||
						pTouchAreaLocalY <= 0)
					{
						switch (m_state)
						{
						case CONTROLABLE:
							m_player.rightClicked = false;
							m_animatedSprite.setCurrentTileIndex(0);
							break;
						default:
							Roller.debugger.showText("status_error");
						}
					}
				return true;
        	}
		};
		m_greySprite = new Sprite(centerX, centerY, this.m_greyTextureRegion);
		m_animatedSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_animatedSprite.setAlpha(0);
		m_animatedSprite.setScale(Button.SIZE[NOW_SIZE]);
		m_greySprite.setScale(Button.SIZE[NOW_SIZE]);
		m_greySprite.setVisible(false);
		scene.registerTouchArea(m_animatedSprite);
		scene.attachChild(m_greySprite);
		scene.attachChild(m_animatedSprite);
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_animatedSprite);
		scene.unregisterTouchArea(m_animatedSprite);
		m_animatedSprite = null;
		
	}

	@Override
	public void setByCameraPosition(float cameraX, float cameraY) {
		m_animatedSprite.setPosition(cameraX + m_onScreenPositionX - Roller.CAMERA_WIDTH / 2.0f,
	 			 		     		 cameraY + m_onScreenPositionY - Roller.CAMERA_HEIGHT / 2.0f);
		m_greySprite.setPosition(cameraX + m_onScreenPositionX - Roller.CAMERA_WIDTH / 2.0f,
	 		     cameraY + m_onScreenPositionY - Roller.CAMERA_HEIGHT / 2.0f);
		
	}
	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void initializeDirection(Direction direction) {
		int centerX;
        int centerY;
        
        float[] tempPosX = m_positionXs;
		float[] tempPosY = m_positionYs;
		switch (Button.NOW_SIZE)
		{
		case 0:
			tempPosX = m_positionXs;
			tempPosY = m_positionYs;
			break;
		case 1:
			tempPosX = m_position1Xs;
			tempPosY = m_position1Ys;
			break;
		case 2:
			tempPosX = m_position2Xs;
			tempPosY = m_position2Ys;
			break;
		}
		switch (direction)
		{
		case UP:
			centerX = (int)(tempPosX[0] - this.m_animatedSprite.getWidth() / 2);
	        centerY =  (int)(tempPosY[0] - this.m_animatedSprite.getHeight() / 2);
	        m_onScreenPositionX = centerX;
	        m_onScreenPositionY = centerY;
			m_direction = Direction.UP;
			m_animatedSprite.setRotation(0);
			m_greySprite.setRotation(0);
			break;
		case DOWN:
			centerX = (int)(tempPosX[3] - this.m_animatedSprite.getWidth() / 2);
		    centerY =  (int)(tempPosY[3] - this.m_animatedSprite.getHeight() / 2);
		    m_onScreenPositionX = centerX;
		    m_onScreenPositionY = centerY;
			m_direction = Direction.DOWN;
			m_animatedSprite.setRotation(180);
			m_greySprite.setRotation(180);
			break;
		case LEFT:
			centerX = (int)(tempPosX[2] - this.m_animatedSprite.getWidth() / 2);
		    centerY =  (int)(tempPosY[2] - this.m_animatedSprite.getHeight() / 2);
		    m_onScreenPositionX = centerX;
	        m_onScreenPositionY = centerY;
			m_direction = Direction.LEFT;
			m_animatedSprite.setRotation(90);
			m_greySprite.setRotation(90);
			break;
		case RIGHT:
			centerX = (int)(tempPosX[1] - this.m_animatedSprite.getWidth() / 2);
	        centerY =  (int)(tempPosY[1] - this.m_animatedSprite.getHeight() / 2);
	        m_onScreenPositionX = centerX;
	        m_onScreenPositionY = centerY;
			m_direction = Direction.RIGHT;
			m_animatedSprite.setRotation(-90);
			m_greySprite.setRotation(-90);
			break;
		}
		Camera tempCamera = Roller.cameraManager.getCamera();
    	setByCameraPosition(tempCamera.getCenterX(), tempCamera.getCenterY());
	}
	@Override
	public void hideButton()
	{
		if(m_animatedSprite.getAlpha() == 0) return;
		if (m_nowModifiler != null)
			m_animatedSprite.unregisterEntityModifier(m_nowModifiler);
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1.0f,m_animatedSprite.getAlpha(),0)));

		m_nowModifiler = EntityModifier;
		m_animatedSprite.registerEntityModifier(m_nowModifiler);
		this.setState(ButtonState.NOT_CONTROLABLE);
		
		m_player.rightClicked = false;
		m_animatedSprite.setCurrentTileIndex(0);
	}
	@Override
	public void setScale(float size)
	{
		m_animatedSprite.setScale(size, size);
		m_greySprite.setScale(size);
	}
	public void hideGreyButton() { m_greySprite.setVisible(false); }
	@Override
	public void showButton()
	{
		if(m_animatedSprite.getAlpha() == 1) return;
		if (m_nowModifiler != null)
			m_animatedSprite.unregisterEntityModifier(m_nowModifiler);
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1.0f, m_animatedSprite.getAlpha(),1)));

		m_nowModifiler = EntityModifier;
		m_animatedSprite.registerEntityModifier(m_nowModifiler);
	}
	
}
